//--------------------------------------------------------------------------------------------------------------
// <copyright file="CSPMyProperty.cs" company="Microsoft Corporation" author="David Martos" date="01/02/2008">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//--------------------------------------------------------------------------------------------------------------
using System;
using System.Data;
using System.Runtime.InteropServices;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

using CSP.LoggingEngine;

namespace CSP.WebParts
{

    /// <summary>
    /// This webpart shows the citizens property info
    /// </summary>
    [Guid("46841C7D-78F1-4eea-B1EF-5D2E31088585")]
    public class CSPMyProperty : BaseWebPart
    {
        #region members       
        protected Label errorLabel;
        private string defaultLatitude = "0.0";
        private string defaultLongitude = "0.0";
        private string address = "";
        private string myPropertyIcon = string.Empty;
        private string myPropertyAddressFormat = string.Empty;
        #endregion

        #region properties
        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)] 
        [ResourcesAttribute("DefaultLatitudeDisplayName", "CSPWebPartParameterCategory", "DefaultLatitudeDescription")]
        public string DefaultLatitude
        {
            get { return this.defaultLatitude; }
            set { this.defaultLatitude = value; }
        }

        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [ResourcesAttribute("DefaultLongitudeDisplayName", "CSPWebPartParameterCategory", "DefaultLongitudeDescription")]
        public string DefaultLongitude
        {
            get { return this.defaultLongitude; }
            set { this.defaultLongitude = value; }
        }

        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [ResourcesAttribute("MyPropertyIconDisplayName", "CSPWebPartParameterCategory", "MyPropertyIconDescription")]
        public string MyPropertyIcon
        {
            get { return this.myPropertyIcon; }
            set { this.myPropertyIcon = value; }
        }

        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [ResourcesAttribute("MyPropertyAddressFormatDisplayName", "CSPWebPartParameterCategory", "MyPropertyAddressFormatDescription")]
        public string MyPropertyAddressFormat
        {
            get { return this.myPropertyAddressFormat; }
            set { this.myPropertyAddressFormat = value; }
        }
        #endregion

        #region methods
        /// <summary>
        /// Constructor
        /// </summary>
        public CSPMyProperty()
        {
            this.ExportMode = WebPartExportMode.All;
        }

        /// <summary>
        /// Gets the citizen address
        /// </summary>
        private void GetAddress()
        {
            CSP.MembershipInterface.MembershipServiceClient client = null;

            try
            {
                string username = Context.User.Identity.Name;

                client = new CSP.MembershipInterface.MembershipServiceClient();
                CSP.MembershipInterface.ObjectModel.Citizen citizen = client.GetCitizenByUsername(username);

                if (citizen != null)
                {
                    this.address = string.Format(this.myPropertyAddressFormat, citizen.Address, citizen.PostalCode, citizen.City);
                }
            }
            catch (InvalidOperationException ioex)
            {
                this.LogMessage(this.LoadResource(WebPartConstants.CSPEndPointErrorMessage), this.GetType().Name, ioex);
                throw new CSP.Core.CSPException(ioex);
            }
            catch (Exception ex)
            {
                this.LogMessage(this.LoadResource(WebPartConstants.CSPMyPropertyLoadDataErrorMessage), this.GetType().Name, ex);
                throw new CSP.Core.CSPException(ex);
            }
            finally
            {
                if (client != null && client.State != System.ServiceModel.CommunicationState.Closed)
                    client.Close();
            }
        }

        /// <summary>
        /// Fired on page create child control, loads the ASCX file
        /// </summary>
        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            try
            {
                if (CSP.Core.MembershipHelper.IsCurrentUserCitizen())
                {
                    this.Hidden = false;

                    GetAddress();

                    UserControls.CSPMyPropertyWUC control = (UserControls.CSPMyPropertyWUC)Page.LoadControl(WebPartConstants.CSPMyPropertyWUC);
                    control.WrapperWebPart = this;
                    control.MyPropertyIcon = MyPropertyIcon; 
                    control.MyPropertyLabel = LoadResource("MyPropertyTitleLabel");
                    control.MyPropertyContent = this.address;
                    control.MyPropertyLatitude = "41.15507023608207";
                    control.MyPropertyLongitude = "-8.61727237701415";

                    this.Controls.Add(control);
                }
                else
                {
                    this.Hidden = true;
                }

            }
            catch (CSP.Core.CSPException cspex)
            {
                errors.Add(this.LoadResource(WebPartConstants.CSPGenericErrorMessage));
            }
            catch (Exception ex)
            {
                this.LogMessage(this.LoadResource(WebPartConstants.CSPLoadControlErrorMessage), this.GetType().Name, ex);
                errors.Add(this.LoadResource(WebPartConstants.CSPGenericErrorMessage));
            }
        }
        #endregion
    }
}
